#region

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

using Volo.Abp;
using Volo.Abp.AspNetCore.MultiTenancy;
using Volo.Abp.Data;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.MultiTenancy.ConfigurationStore;

#endregion

namespace Leo.Core.Shared.Module
{
    /// <summary>
    ///     多租户模块
    ///     TODO：未完成
    /// </summary>
    public class MultiTenantModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            ConfigureTenant(context);
        }

        /// <summary>
        ///     多租户相关配置
        /// </summary>
        /// <param name="context"></param>
        private void ConfigureTenant(ServiceConfigurationContext context)
        {
            context.Services.Configure<AbpMultiTenancyOptions>(options =>
            {
                options.IsEnabled = true;
            });

            context.Services.Configure<AbpAspNetCoreMultiTenancyOptions>(options =>
            {
                //options.TenantKey = "MyTenantKey";
            });

            // 硬编码定义租户
            Configure<AbpDefaultTenantStoreOptions>(options =>
            {
                options.Tenants = new[]
                {
                    new TenantConfiguration(
                        Guid.Parse("446a5211-3d72-4339-9adc-845151f8ada0"), //Id
                        "tenant1" //Name
                    ),
                    new TenantConfiguration(
                        Guid.Parse("25388015-ef1c-4355-9c18-f6b6ddbaf89d"), //Id
                        "tenant2" //Name
                    )
                    {
                        //tenant2 有单独的数据库连接字符串
                        ConnectionStrings = { { ConnectionStrings.DefaultConnectionStringName, "..." } }
                    }
                };
            });
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var env = context.GetEnvironment();
            app.UseMultiTenancy();
        }
    }
}